#!/usr/bin/env python
import os, sys
exe_path = os.path.split(os.path.abspath(sys.argv[0]))[0]
sys.path.append(os.path.abspath(os.path.join(exe_path, "python_modules")))


import marshal, struct


from itertools import izip
import time
t = tuple(izip(range(10),  range(5, 50, 2)))

def marshal_save (t):
    start = time.time()
    cache_file = open("delete.me.cache", "w")
    for i in range(100000):
        marshal.dump(t, cache_file)
    end = time.time()
    print end - start

def marshal_load ():
    start = time.time()
    cache_file = open("delete.me.cache")
    for i in range(100000):
        new_t = marshal.load(cache_file)
    end = time.time()
    print end - start
    return new_t
    
def file_save (t):
    start = time.time()
    cache_file = open("delete.me.cache", "wb")
    for i in range(100000):
        cache_file.write(struct.pack("l", len(t)))
        d = [tt[0] for tt in t]
        cache_file.write(struct.pack("l" * len(t), *d ))
        d = [tt[1] for tt in t]
        cache_file.write(struct.pack("l" * len(t), *d ))
    end = time.time()
    print end - start

def file_load ():
    start = time.time()
    cache_file = open("delete.me.cache", "rb")
    for i in range(100000):
        
        len_t = int(struct.unpack("l", cache_file.read(4))[0])
        d1 = struct.unpack("l"*len_t, cache_file.read(4 * len_t))
        d2 = struct.unpack("l"*len_t, cache_file.read(4 * len_t))
        new_t = tuple(izip(d1, d2))
    end = time.time()
    print end - start
    return new_t
    
marshal_save(t)
new_t = marshal_load()
assert (t == new_t)

file_save(t)
new_t = file_load()
assert (t == new_t)

